"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
/**文件目录处理工具 */
const m_file_1 = tslib_1.__importDefault(require("./utils/m-file"));
/**网络处理工具 */
const m_http_1 = tslib_1.__importDefault(require("./utils/m-http"));
/** 进度显示工具 */
const m_spinner_1 = tslib_1.__importDefault(require("./utils/m-spinner"));
/**命令处理器业务 */
const m_cmder_1 = tslib_1.__importDefault(require("./services/m-cmder"));
/**swagger解析器业务 */
const m_parser_1 = tslib_1.__importDefault(require("./services/m-parser"));
/**文件书写器业务 */
const m_writer_1 = tslib_1.__importDefault(require("./services/m-writer"));
/**主流程处理 */
class MMain {
    /**命令参数 */
    static parm = {};
    /**解析Npm命令参数 */
    static async getParm() {
        m_spinner_1.default.start("正在分析命令参数...");
        const parm = await m_cmder_1.default.getParm();
        Object.assign(MMain.parm, parm);
        m_spinner_1.default.success("分析命令参数完成...");
        return MMain;
    }
    /**加载Swagger配置 */
    static async loadJson() {
        const { swgFile, swgUrl, swgJson } = MMain.parm;
        const { offline, authUser, authPass } = MMain.parm;
        const baseAuth = { user: authUser, pass: authPass };
        let swg = {};
        m_spinner_1.default.start("正在加载swagger数据...");
        if (offline) {
            const res = await m_file_1.default.readFile(swgFile);
            swg = JSON.parse(res);
        }
        else {
            const res = await m_http_1.default.get(swgUrl, null, { baseAuth });
            swg = res.data;
        }
        Object.assign(swgJson, swg);
        m_spinner_1.default.success("加载swagger数据完成...");
        return MMain;
    }
    /**解析Swagger数据成格式化数据 */
    static async parseSwg() {
        const parm = MMain.parm;
        m_spinner_1.default.start("正在解析swagger数据...");
        const result = new m_parser_1.default(parm).parse();
        parm.result = result;
        m_spinner_1.default.success("解析swagger数据完成...");
        return MMain;
    }
    /**把格式化数据写成文件 */
    static async writeFiles() {
        const parm = MMain.parm;
        m_spinner_1.default.start("正在书写接口模型文件...");
        await m_writer_1.default.write(parm);
        m_spinner_1.default.success("书写接口模型文件完成...");
        return MMain;
    }
    /**任务处理成功 */
    static async success() {
        const { apiPath } = MMain.parm;
        m_spinner_1.default.success(`Api发布成功！请查看目录：${apiPath}`);
    }
    /**任务处理失败 */
    static async fail(err) {
        const msg = MMain.getError(err);
        m_spinner_1.default.fail(msg);
    }
    /**输出日志 */
    static async console() {
        console.log(MMain.parm);
        return MMain;
    }
    /**获取异常信息 */
    static getError(err) {
        const isDate = err instanceof Date;
        const isArr = err instanceof Array;
        const isObj = Object.prototype.toString.call(err) == "[object Object]";
        if (isDate)
            return new Date(err).toString();
        if (isArr)
            return err[0] || "";
        if (isObj)
            return err.message || `${err}`;
        return `${err}`;
    }
}
exports.default = MMain;
